﻿#define DEBUG
//#define RELEASE

using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;

using SuperMapLib;

namespace JR_RouteAlarm
{
    class Examples
    {
        public Examples(soDatasets objDSs)
        {
            //DivideArea(objDSs);
        }

        private void DivideArea(soDatasets objDSs)
        {
            //FindDividingPoints(objDSs);
        }

        public soPoints FindDividingPoints(soDatasets objDSs)
        {
            //if (objDSs != null && objDSs.Count > 0)
            //{
                soDataset objDSRegion = null;
                soDataset objDSLine = null;
                soDataset objDSPoint = null;
                soDatasetVector objDatasetV = null;
                soRecordset objRS = null;
                soGeoRegion objRegion = null;
                soGeoLine objLine = null;
                soPoints tmpPoints = null;
                soGeoPoint objPointTmp = null;
                //soGeoPoint[] objPointsDividing = null;
                soGeoPoint[] objPointsCross = null;
                //soGeoPoint[] objPointsBoundary = null;

                objDSLine = objDSs[Utility.ROUTE_DATASET_NAME];
                objDatasetV = objDSLine as soDatasetVector;
                objRS = objDatasetV.Query("SmID = 25625",   // + " and SmUserID = 326 and " +
                    //Route.FEATURE_CD + " = '0102' and " +
                    //Route.DAIBUN_CD + " = '01' and " +
                    //Route.TSUUSHO_CD + " = '476'",
                    true, null, "");
                if (objRS !=null && !objRS.IsBOF())
                {
                    objRS.MoveFirst();
                }
                objLine = objRS.GetGeometry() as soGeoLine;
                objRS.Close();
                objDatasetV.Close();

                objDatasetV.ClearRecordsets();
                objDSRegion = objDSs["Buffer_武蔵野線3"];
                objDatasetV = objDSRegion as soDatasetVector;
                objRS = objDatasetV.Query("-1 < 0", true, null, "");
                if (objRS != null && !objRS.IsBOF())
                {
                    objRS.MoveFirst();
                }
                objRegion = objRS.GetGeometry() as soGeoRegion;
                objRS.Close();
                objDatasetV.Close();

                tmpPoints = objLine.GetCrossings(objRegion as soGeometry);
                objPointsCross = new soGeoPoint[2];
                objPointsCross[0] = new soGeoPoint();
                objPointsCross[1] = new soGeoPoint();
                objPointsCross[0].x = tmpPoints[1].x;
                objPointsCross[0].y = tmpPoints[1].y;
                objPointsCross[1].x = tmpPoints[2].x;
                objPointsCross[1].y = tmpPoints[2].y;
                tmpPoints.RemoveAll();

                objDatasetV.ClearRecordsets();
                objDSPoint = objDSs["キロ標_大宮"];
                objDatasetV = objDSPoint as soDatasetVector;
                //objPointsBoundary = new soGeoPoint[4];
                //objPointsDividing = new soGeoPoint[2];
                for (int i = 0; i < 2; i++)
                {
                    objRS = objDatasetV.QueryByDistance(objPointsCross[i] as soGeometry, 100, "");
                    if (objRS != null && !objRS.IsBOF())
                    {
                        objRS.MoveFirst();
                    }
                    //objPointsDividing[i] = new soGeoPoint();
                    //for (int j = i * 2; !objRS.IsEOF(); objRS.MoveNext(), j++)
                    for (; !objRS.IsEOF(); objRS.MoveNext())
                    {
#if DEBUG
                        objPointTmp = objRS.GetGeometry() as soGeoPoint;
                        Console.WriteLine("x={0}, y={1}, ", objPointTmp.x, objPointTmp.y);
#endif
                        if (!objRegion.SpatialRelation.Contains(objRS.GetGeometry()))
                        {
#if RELEASE
                            objPointTmp = objRS.GetGeometry() as soGeoPoint;
#endif
                            //objPointsDividing[i] = objPointTmp;
                            tmpPoints.Add2(objPointTmp.x, objPointTmp.y);
                        }
                    }
                    objRS.Close();
                }
                objDatasetV.Close();
                objDatasetV.ClearRecordsets();


                Marshal.ReleaseComObject(objDatasetV);
                objDatasetV = null;
                Marshal.ReleaseComObject(objRS);
                objRS = null;
                //for (int i = 0; i < 4; i++)
                //{
                //    Marshal.ReleaseComObject(objPointsBoundary[i]);
                //    objPointsBoundary[i] = null;
                //}
                Marshal.ReleaseComObject(objPointsCross[0]);
                objPointsCross[0] = null;
                Marshal.ReleaseComObject(objPointsCross[1]);
                objPointsCross[1] = null;
                //Marshal.ReleaseComObject(objPointsDividing[0]);
                //objPointsDividing[0] = null;
                //Marshal.ReleaseComObject(objPointsDividing[1]);
                //objPointsDividing[1] = null;
                Marshal.ReleaseComObject(objRegion);
                objRegion = null;
                Marshal.ReleaseComObject(objLine);
                objLine = null;
                Marshal.ReleaseComObject(objDSPoint);
                objDSPoint = null;
                Marshal.ReleaseComObject(objDSLine);
                objDSLine = null;
                Marshal.ReleaseComObject(objDSRegion);
                objDSRegion = null;

                return tmpPoints;
            //}
        }
    }
}
